package cn.founder.org.controller;

import cn.founder.basic.exception.BusinessException;
import cn.founder.basic.util.AjaxResult;
import cn.founder.basic.util.PageList;
import cn.founder.org.domain.Shop;
import cn.founder.org.query.ShopQuery;
import cn.founder.org.service.IShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/shop")
public class ShopController {

    @Autowired
    private IShopService shopService;

    //店铺入驻注册
    @PostMapping("/settlement")
    public AjaxResult settlement(@RequestBody Shop shop){
        try {
            //自定义 一个方法  内部含有其他的业务 比如验证客户信息是否重复等.
            shopService.settlement(shop);
            return AjaxResult.myself();
        } catch (BusinessException e) {
            //使用自定义类   获取后端传来的具体错误信息
            e.printStackTrace();
            return AjaxResult.fail(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.fail("系统繁忙，请稍后再试。");
        }
    }

    //查询一条数据
    @GetMapping("/{id}")
    public Shop queryOne(@PathVariable("id") Long id){
        return shopService.queeryById(id);
    }

    //查询所有
    @GetMapping
    public List<Shop> queryAll(){
        return shopService.queryAll();
    }

    //删除一条数据
    @DeleteMapping("/{id}")
    public AjaxResult delOne(@PathVariable("id") Long id){
        try {
            shopService.del(id);
            return AjaxResult.myself();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.fail("删除失败");
        }
    }

    //添加或修改
    @PutMapping
    public AjaxResult addorUpdata(@RequestBody Shop shop){
        try {
            if (shop.getId()==null){
                shopService.add(shop);
            }else {
                shopService.update(shop);
            }
            return  AjaxResult.myself();
        } catch (Exception e) {
            e.printStackTrace();
            return  AjaxResult.fail("操作失败");
        }
    }

    //分页查询
    @PostMapping
    public PageList<Shop> queryPage(@RequestBody ShopQuery query){
        return shopService.queryPage(query);
    }



    //批量删除一条数据
    @PatchMapping
    public AjaxResult pathchDelete(@RequestBody List<Long> ids){
        try {
            shopService.pathchDel(ids);
            return AjaxResult.myself();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.fail("删除失败");
        }
    }

}
